<?php

use Illuminate\Support\Facades\DB;


/**
 * 获取真实IP地址
 */
function getRealIp()
{
    $ip = false;
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
        $ips = explode(", ", getenv("HTTP_X_FORWARDED_FOR"));
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = false;
        }
        $ipscount = count($ips);
        for ($i = 0; $i < $ipscount; $i++) {
            if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
        $ip = getenv("REMOTE_ADDR");
    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
        $ip = $_SERVER['REMOTE_ADDR'];
    } else {
        $ip = "unknown";
    }
    return isIp($ip) ? $ip : "unknown";
}
/**
 * 检查是否是合法的IP
 */
function isIp($ip)
{
    if (preg_match('/^((\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)(?:\.(\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)){3})$/', $ip)) {
        return true;
    } else {
        return false;
    }
}
/**
 * 验证手机号
 */
function checkMobile($mobile)
{
    return preg_match('/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/i', $mobile);
}


//更新支付状态
function update_pay_status($data){

    $order_sn = $data['out_trade_no'];



    if(stripos($order_sn,'recharge') !== false){//会员充值订单
        $order =  DB::table('user_recharge')->where('order_sn','=',$order_sn)->first();
        //修改支付状态
        DB::table('user_recharge')->where('order_sn','=',$order_sn)->update(['pay_status'=>1,'pay_time'=>time(),'xml'=>json_encode($data)]);
        //钱包加钱
        DB::table('users')->where('id','=',$order->user_id)->update(['money'=>DB::raw("money + $order->money"),'send_money' => DB::raw("send_money + $order->send_money")]);

    }else{

        $order =  DB::table('order')->where('order_sn','=',$order_sn)->first();
        if($order->pay_status < 1){

            //修改订单支付状态  修改订单支付时间
            DB::table('order')->where('order_sn',$order_sn)->update(['pay_status'=>1,'order_status'=>1,'pay_time'=>date("Y-m-d H:i:s")]);


            //减少库存
            $order_goods = DB::table('order_goods')->where('order_id',$order->id)->get();
            foreach ($order_goods as $k=>$v){

                DB::table('goods')->where('id',$v->goods_id)->decrement('stock',$v->num);
            }


            $order = (array)$order;
            //打印标签
            $print = new \App\Http\Controllers\Api\PrintController();

            $print->PrintOrder($order['id']);

            sleep(2);

            //订单发货
            $shipping = new \App\Http\Controllers\Api\ShippingController();

            $shipping->sendDelivery($order);

        }


    }


}










